package com.pucong.gitee.database.backup.business.unRelational.redis.service;

import com.pucong.gitee.database.backup.business.unRelational.redis.cover.AutoRedisBeanConvert;
import com.pucong.gitee.database.backup.business.unRelational.redis.mapper.AutoRedisMapper;
import com.pucong.gitee.database.backup.business.unRelational.redis.util.ClusterUtil;
import com.pucong.gitee.database.backup.business.unRelational.redis.util.JschUtil;
import com.pucong.gitee.database.backup.business.unRelational.redis.util.RedisDataUtil;
import com.pucong.gitee.database.backup.common.entity.redis.AutoRedisInfo;
import com.pucong.gitee.database.backup.common.entity.redis.RedisConnect;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.common.selfenum.CommonBoolenEnum;
import com.pucong.gitee.database.backup.configuration.exception.BusinessException;
import com.pucong.gitee.database.backup.utils.RedisUtil;
import com.pucong.gitee.database.backup.utils.UserRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;


@Service
@Slf4j
public class AutoRedisConnectServiceImpl implements AutoRedisConnectService {

    @Autowired
    private AutoRedisMapper autoRedisMapper;

    @Autowired
    private AutoRedisBeanConvert autoRedisBeanConvert;

    @Override
    public Jedis getJedis(String redisId) {
        UserJwt loginUser = UserRequest.getLoginUser();
        AutoRedisInfo autoRedisInfo = autoRedisMapper.selectById(redisId);
        RedisConnect redisConnect = autoRedisBeanConvert.do2dto(autoRedisInfo);
        if (CommonBoolenEnum.是 == autoRedisInfo.getConnectType()) {
            try {
                JschUtil.openSSH(redisConnect);
            } catch (Exception e) {
                throw new BusinessException("打开SSH连接失败");
            }
        }
        // 单机
        if (autoRedisInfo.getIsHa() == CommonBoolenEnum.否) {
            Jedis jedis = RedisUtil.getJedis(redisConnect);
            return jedis;
        } else {
            throw new BusinessException("打开连接失败");
        }
    }

    @Override
    public JedisCluster getJedisCluster(String redisId) {
        UserJwt loginUser = UserRequest.getLoginUser();
        AutoRedisInfo autoRedisInfo = autoRedisMapper.selectById(redisId);
        RedisConnect redisConnect = autoRedisBeanConvert.do2dto(autoRedisInfo);
        if (CommonBoolenEnum.是 == autoRedisInfo.getConnectType()) {
            try {
                JschUtil.openSSH(redisConnect);
            } catch (Exception e) {
                throw new BusinessException("打开SSH连接失败");
            }
        }
        // 集群
        if (autoRedisInfo.getIsHa() == CommonBoolenEnum.是) {
            JedisCluster cluster = ClusterUtil.openCulter(redisConnect);
            return cluster;
        } else {
            throw new BusinessException("打开连接失败");
        }
    }

    @Override
    public RedisConnect getRedisConnect(String redisId) {
        UserJwt loginUser = UserRequest.getLoginUser();
        RedisConnect redisConnect = RedisDataUtil.getRedisConnect(loginUser.getUuid(), redisId);
        if (redisConnect == null) {
            AutoRedisInfo autoRedisInfo = autoRedisMapper.selectById(redisId);
            redisConnect = autoRedisBeanConvert.do2dto(autoRedisInfo);
            RedisDataUtil.setRedisConnect(loginUser.getUuid(), redisId, redisConnect);
        }
        return redisConnect;
    }

    @Override
    public void clearConfig(String id) {
        UserJwt loginUser = UserRequest.getLoginUser();
        RedisDataUtil.clearConfig(loginUser.getUuid(), id);
    }

}
